#include "autopilot_comm.h"

/*!@brief Returns the rotomotion message type number for the next packet.
 *
 * @param socket The socket to pull the data out of
 * @return The autopilot message type number in host-byte ordering
 *
 * This function peeks at the socket so that we know what function to use to pull
 * the full packet. This function will block until a message comes in unless the
 * socket options say otherwise. It makes no allowences for a nonblocking call
 * and will not return good data if it is called in this manner. If you require 
 * non-blocking behavior only call this function when select says there is data waiting
 * on the socket.
 */
uint32_t get_message_type(int socket) {

    msg_base temp;

    /* get the data from the network using MSG_PEEK. This data will
     * still be there when the whole packet is pulled.
     */
    recvfrom(socket, (void*) &temp, sizeof(msg_base), MSG_PEEK, NULL, NULL);

    /* Could check to see that the whole packet was receved like this:
     * if(ret != sizeof(msg_base))
     * Unfortunatly there is no good way to return the error up the stack
     * without making this an unnesisarally complicated function to use.
     */

    return ntohl(temp.type);
} /* end get_message_type */
